###############################################################
###############################################################
###############################################################
# Functions to do analysis of evidence table
# and then add columns to evidence table
# Friday 16 Dec

###############################################################
# This function does the risk ration calculations for the
# measured outcomes and adds them to the end of each
# dictionary of the evidence table

def addriskratio(db1):
    mydb2 = []
    for x in db1:
        #print x
        rq1 = "NA"
        rq2 = "NA"
        rq3 = "NA"
        rq5 = "NA"
        tqd = "NA"
        tq1 = "NA"
        tq2 = "NA"
        tq3 = "NA"
        mq1 = "NA"
        mq2 = "NA"
        
        if x['1yrOS_A'] != 'NA' and x['1yrOS_B'] != 'NA':
            r1 = float(x['1yrOS_A'])/float(x['1yrOS_B'])
            if r1 > 1:
                rq1 = "sup"
            elif r1 < 1:
                rq1 = "inf"
            else:
                rq1 = "eq"
            r1 = str(round(r1,4))
            x['1YrRiskRatio'] = r1
        x['1YrRiskRatioDirection'] = rq1
        if x['2yrOS_A'] != 'NA' and x['2yrOS_B'] != 'NA':
            r2 = float(x['2yrOS_A'])/float(x['2yrOS_B'])
            if r2 > 1:
                rq2 = "sup"
            elif r2 < 1:
                rq2 = "inf"
            else:
                rq2 = "eq"
            r2 = str(round(r2,4))
            x['2YrRiskRatio'] = r2
        x['2YrRiskRatioDirection'] = rq2
        
        if x['3yrOS_A'] != 'NA' and x['3yrOS_B'] != 'NA':
            r3 = float(x['3yrOS_A'])/float(x['3yrOS_B'])
            if r3 > 1:
                rq3 = "sup"
            elif r3 < 1:
                rq3 = "inf"
            else:
                rq3 = "eq"
            r3 = str(round(r3,4))
            x['3YrRiskRatio'] = r3
        x['3YrRiskRatioDirection'] = rq3
        
        if x['5YrOS_A'] != 'NA' and x['5YrOS_B'] != 'NA':
            r5 = float(x['5YrOS_A'])/float(x['5YrOS_B'])
            if r5 > 1:
                rq5 = "sup"
            elif r5 < 1:
                rq5 = "inf"
            else:
                rq5 = "eq"
            r5 = str(round(r5,4))
            x['5YrRiskRatio'] = r5
        x['5YrRiskRatioDirection'] = rq5

        if x['MedianOS_A'] != 'NA' and x['MedianOS_B'] != 'NA':
            m1 = float(x['MedianOS_A'])/float(x['MedianOS_B'])
            if m1 > 1:
                mq1 = "sup"
            elif m1 < 1:
                mq1 = "inf"
            else:
                mq1 = "eq"
            m1 = str(round(m1,4))
            x['MedianOSRiskRatio'] = m1
        x['MedianOSRiskRatioDirection'] = mq1

        if x['ResponseRate_A'] != 'NA' and x['ResponseRate_B'] != 'NA':
            m2 = float(x['ResponseRate_A'])/float(x['ResponseRate_B'])
            if m2 > 1:
                mq2 = "sup"
            elif m2 < 1:
                mq2 = "inf"
            else:
                mq2 = "eq"
            m2 = str(round(m2,4))
            x['ResponseRateRiskRatio'] = m2
        x['ResponseRateRiskRatioDirection'] = mq2

        if x['ToxicDeathRate_A'] != 'NA' and x['ToxicDeathRate_B'] != 'NA':
            try:
                td = float(x['ToxicDeathRate_A'])/float(x['ToxicDeathRate_B'])
                if td > 1:
                    tqd = "inf"
                elif td < 1:
                    tqd = "sup"
                else:
                    tqd = "eq"
                td = str(round(td,4))
                x['ToxicDeathRiskRatio'] = td
                x['ToxicDeathRiskRatioDirection'] = tqd
            except ZeroDivisionError:
                x['ToxicDeathRiskRatio'] = "NA"
                x['ToxicDeathRiskRatioDirection'] = "eq"

        if x['Grd3Neutropenia_A'] != 'NA' and x['Grd3Neutropenia_B'] != 'NA':
            #t1 = doratio(float(myclean(x['Grd3Neutropenia_A'])),float(myclean(x['Grd3Neutropenia_B'])))
            t1 = doratio(x['Grd3Neutropenia_A'],x['Grd3Neutropenia_B'])
            #print t1
            if t1 > 1:
                tq1 = "inf"
            elif t1 < 1:
                tq1 = "sup"
            else:
                tq1 = "eq"
            t1 = str(round(t1,4))
            x['Grd3NeutropeniaRiskRatio'] = t1
        x['Grd3NeutropeniaRiskRatioDirection'] = tq1

        if x['Grd3OesTox_A'] != 'NA' and x['Grd3OesTox_B'] != 'NA':
            #t1 = doratio(float(myclean(x['Grd3Neutropenia_A'])),float(myclean(x['Grd3Neutropenia_B'])))
            t2 = doratio(x['Grd3OesTox_A'],x['Grd3OesTox_B'])
            #print t1
            if t2 > 1:
                tq2 = "inf"
            elif t2 < 1:
                tq2 = "sup"
            else:
                tq2 = "eq"
            t2 = str(round(t2,4))
            x['Grd3OesToxRiskRatio'] = t2
        x['Grd3OesToxRiskRatioDirection'] = tq2

        if x['Grd3PneumoTox_A'] != 'NA' and x['Grd3PneumoTox_B'] != 'NA':
            #t1 = doratio(float(myclean(x['Grd3Neutropenia_A'])),float(myclean(x['Grd3Neutropenia_B'])))
            t3 = doratio(x['Grd3PneumoTox_A'],x['Grd3PneumoTox_B'])
            #print t1
            if t3 > 1:
                tq3 = "inf"
            elif t3 < 1:
                tq3 = "sup"
            else:
                tq3 = "eq"
            t3 = str(round(t3,4))
            x['Grd3PneumoToxRiskRatio'] = t3
        x['Grd3PneumoToxRiskRatioDirection'] = tq3
        
        mydb2.append(x)
    return mydb2
        #print x




def doratio(a,b):
    x = myclean(a)
    y = myclean(b)
    if x != 0 and y != 0:
        return x/y
    if x != 0 and y == 0:
        return 100000000
    if x == 0 and y != 0:
        return 0
    if x == 0 and y == 0:
        return 1

def myclean(x):
    if "%" in x:
        y = x[0:2]
        y = float(y)/100
    else:
        y = float(x)
    return y

    

#########################################
#########################################

